package com.API.beans;

import com.API.dal.entities.Location;
import com.API.dal.entities.Objects;
import com.API.dal.providers.CustomerProvider;
import com.API.dal.providers.LocationProvider;
import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.LinkedList;
import java.util.List;

@Component
@Scope("request")
public class CustomerBean {

    @Autowired
    CustomerProvider customerProvider;
    @Autowired
    LocationProvider locationProvider;

    List<Objects> customerList;
    Objects customer = new Objects();
    Objects selectedCustomer;
    List<Location> locationList;
    Location location = new Location();

    public List<Objects> getCustomerList() {
        if (customerList == null)
            customerList = customerProvider.getCustomers();
        return customerList;
    }

    public Objects getCustomer() {
        return customer;
    }

    public void setCustomer(Objects customer) {
        this.customer = customer;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    public List<String> getLocationList() {
        List<String> locList = new LinkedList<String>();
        if (locationList == null)
            locationList = locationProvider.getLocations();
        for (Location loc: locationList)
            locList.add(loc.getName());
        return locList;
    }

    public Objects getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Objects selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public void saveCustomer(){
        RequestContext context = RequestContext.getCurrentInstance();
        context.update("customersPage");
        location = locationProvider.getLocationByName(location.getName());
        customer.setLocation(location);
        customerProvider.addCustomer(customer);
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Заказчик добавлен успешно", customer.getName());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("saveCustomer", true);

    }


     public void addCustomer(){
         final RequestContext context = RequestContext.getCurrentInstance();
         context.execute("addCustomerDialog.show()");
     }


    public void editCustomer(){
        if (customerList.contains(selectedCustomer)){
            customer = selectedCustomer;
            final RequestContext context = RequestContext.getCurrentInstance();
            context.execute("editCustomerDialog.show()");
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Ошибка!",
                    "Заказчик не выбран!"));
        }
    }



    public void viewCustomer(){

    }

}
